Skip to content

Prepare handles describe failure#2418

Merged
jackc merged 3 commits into
masterfrom
prepare-handles-describe-failure
Nov 1, 2025
Merged

Prepare handles describe failure#2418
jackc merged 3 commits into
masterfrom
prepare-handles-describe-failure

Conversation

@jackc

@jackc jackc commented Nov 1, 2025

Copy link
Copy Markdown
Owner

This handles a Prepare() that fails after ParseComplete but before receiving the complete statement description. Previously, this could leave a prepared statement on the server without pgx knowing about the statement. This could cause repeated failures as pgx continued to try to prepare the statement and failed.

Solved by detecting that error specifically and deallocating the statement during the next Prepare.

The same error in batches was already handled via invalidating the newly prepared statements in the statement cache.

Also, introduces faultyconn package which allows introducing difficult to reproduce errors into the PostgreSQL connection. This allowed creating a delay between the Parse and Describe messages which reliably reproduced this error in tests.

faultyconn required adding AfterNetConnect hook to pgconn.Config. While this is mostly useful for testing, it could also be useful for logging or production diagnostics.

Fixes #2223
Supersedes #2411.

jackc added 3 commits October 31, 2025 18:26
Allows wrapping or modifying net.Conn after network connection
establishment but before PostgreSQL protocol communication begins.
A failure that occurred after a successful Parse could leave a prepared
statement on the server that must be deallocated before re-preparing.

Also, introduce a testing utility faultyconn.Conn that can be used to
intercept and modify frontend messages. This is used to induce difficult
difficult to reproduce timing and protocol errors in tests.
@jackc

jackc commented Nov 1, 2025

Copy link
Copy Markdown
Owner Author

@oschwald This approach is a bit more elegant than my previous PR. The new faultyconn test facility this prompted me to write should also aid in reproducing other rare cases in tests.

@jackc jackc merged commit eea5c50 into master Nov 1, 2025
12 of 14 checks passed
@oschwald

oschwald commented Nov 2, 2025

Copy link
Copy Markdown

Thank you! We will try it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Occasional prepared statement already exist errors when using statement timeout

2 participants